package com.todo.sight.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.sight.infrastructure.mapper.CityInfoMapper;
import com.todo.sight.infrastructure.entity.condition.CityInfoCondition;
import com.todo.sight.infrastructure.entity.po.CityInfoPO;
import com.todo.sight.infrastructure.repository.CityInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (CityInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:03
 */
@Slf4j
@Repository("cityInfoRepository")
public class CityInfoRepositoryImpl implements CityInfoRepository {

    @Resource
    private CityInfoMapper cityInfoMapper;


    @Override
    public Boolean updateCityInfoPOById(CityInfoPO cityInfoPO) {
        if (cityInfoPO == null || cityInfoPO.getId() == null) {
            return false;
        }
        if (!baseFieldCheck(cityInfoPO)) {
            return false;
        }
        return cityInfoMapper.updateCityInfoPOById(cityInfoPO) > 0;
    }

    @Override
    public Boolean removeCityInfoPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return cityInfoMapper.removeCityInfoPOById(id) > 0;
    }

    @Override
    public PageResult<CityInfoPO> queryCityInfoByCondition(CityInfoCondition cityInfoCondition) {
        PageResult<CityInfoPO> pageResult = new PageResult<>();
        if (cityInfoCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(cityInfoCondition.getPageIndex());
        pageResult.setPageSize(cityInfoCondition.getPageSize());
        long count = queryCountByCondition(cityInfoCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, cityInfoCondition.getPageSize()));
        pageResult.setTotal(count);
        List<CityInfoPO> cityInfoPOList = cityInfoMapper.queryCityInfoByCondition(cityInfoCondition);
        if (CollectionUtils.isEmpty(cityInfoPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(cityInfoPOList);
        pageResult.setCurrentIndex(cityInfoPOList.get(0).getId());
        pageResult.setHasNext(cityInfoPOList.size() >= cityInfoCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(CityInfoCondition cityInfoCondition) {
        if (cityInfoCondition == null || cityInfoCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return cityInfoMapper.queryCountByCondition(cityInfoCondition);
    }

    @Override
    public CityInfoPO queryCityInfoPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return cityInfoMapper.queryCityInfoPOById(id);
    }

    @Override
    public Boolean insertCityInfoPO(CityInfoPO cityInfoPO) {
        if (cityInfoPO == null) {
            return false;
        }
        if (!baseFieldCheck(cityInfoPO)) {
            return false;
        }
        if (cityInfoPO.getLat() == null || cityInfoPO.getLon() == null) {
            return false;
        }
        return cityInfoMapper.insertCityInfoPO(cityInfoPO) > 0;
    }

    @Override
    public List<CityInfoPO> searchCityInfoByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return cityInfoMapper.searchCityInfoPOByName(name);
    }

    @Override
    public CityInfoPO queryCityInfoByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return cityInfoMapper.queryCityInfoPOByName(name);
    }

    @Override
    public List<CityInfoPO> queryCityInfoListByParentId(Long parentId) {
        if (parentId == null || parentId < 0) {
            return Collections.emptyList();
        }
        return cityInfoMapper.queryCityInfoListByParentId(parentId);
    }

    private boolean baseFieldCheck(CityInfoPO cityInfoPO) {
        if (cityInfoPO == null) {
            return false;
        }
        if (cityInfoPO.getName() == null || cityInfoPO.getType() == null || cityInfoPO.getIsShow() == null) {
            log.warn("CityInfoRepository#baseFieldCheck 基础字段校验失败，默认操作失败 cityInfoPO：【{}】", JSONObject.toJSONString(cityInfoPO));
            return false;
        }
        return true;
    }
}
